package com.casicloud.manage.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.casicloud.manage.model.Authority;
import com.casicloud.manage.model.Menu;
import com.casicloud.manage.model.Role;
import com.casicloud.manage.model.User;
import com.casicloud.manage.model.UserRole;
import com.casicloud.manage.service.AuthorityService;
import com.casicloud.manage.service.MenuService;
import com.casicloud.manage.service.RoleService;
import com.casicloud.manage.service.UserRoleService;
import com.casicloud.manage.service.UserService;
import com.common.sub.util.ControllerBase;
import com.common.sub.util.JsonUtils;
import com.common.sub.util.PageUtil;
import com.common.sub.util.ZtreeModel;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping(value = "role")
public class RoleController extends ControllerBase{
	
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private AuthorityService authorityService;
	
	@Autowired
	private UserService userService;
	@Autowired
	private UserRoleService userRoleService;
	
	
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(HttpServletRequest request,HttpServletResponse response){
		return "role/list";
	}
	
	@RequestMapping(value = "query")
	public String query(Role role,PageUtil pageUtil,HttpServletRequest request,HttpServletResponse response){
		PageInfo<Role>	page  = roleService.findByPage(pageUtil,role);
		return this.dataToJson(page);
	}
	
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String goadd(Role role,HttpServletRequest request,HttpServletResponse response){
		return "role/add";
	}
	
	
	@RequestMapping(value = "allotauthority", method = RequestMethod.GET)
	public String allotauthority(Role role,HttpServletRequest request,HttpServletResponse response){
		
		
		
		role= roleService.findByOne(role);
		
		List<Integer> rlist = JSON.parseArray(role.getPermissions(), Integer.class);
		
		
		
		Menu menu = new Menu();
		menu.setIsAvailable(1);
		List<Menu> mlist = menuService.findByList(menu);
		Authority authority = new Authority();
		List<Authority> alist = authorityService.findByList(authority);
		
		List<ZtreeModel> zlist = new ArrayList<ZtreeModel>();
		
		
		for(int i = 0;i<mlist.size();i++){
			Menu m= mlist.get(i);
			ZtreeModel zm = new ZtreeModel();
			zm.setId(m.getId());
			if(rlist.contains(m.getId())){
				zm.setChecked(true);
				zm.setOpen(true);
			}
			zm.setName(m.getName());
			zm.setIcon(m.getIcon());
			zm.setpId(m.getPid());
			zlist.add(zm);
		}
		
		for(int i = 0;i<alist.size();i++){
			ZtreeModel zm = new ZtreeModel();
			Authority a = alist.get(i);
			zm.setId(0-a.getId());
			
			if(rlist.contains(0-a.getId())){
				zm.setChecked(true);
				zm.setOpen(true);
			}
			
			zm.setName(a.getName());
			zm.setIcon("img/icon/3.png");
			zm.setpId(a.getMenuId());
			zlist.add(zm);
			
		}
		request.setAttribute("json",JsonUtils.ObjectToString(zlist));
		
		return "role/authority";
	}
	
	@RequestMapping(value = "allotauthority", method = RequestMethod.POST)
	public String allotauthority(Role role,@RequestParam("authorityId")Integer[] ids,HttpServletRequest request,HttpServletResponse response){

		role.setPermissions(JSON.toJSONString(ids));
		roleService.update(role);
		return this.formSuccee();
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String doadd(Role role,HttpServletRequest request,HttpServletResponse response){
		roleService.save(role);
		
		return this.formSuccee();
	}
	
	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String goupdate(Role role,HttpServletRequest request,HttpServletResponse response){
		role = roleService.findByOne(role);
		request.setAttribute("r", role);
		return "role/update";
	}
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String doupdate(Role role,HttpServletRequest request,HttpServletResponse response){
		
		roleService.update(role);
		
		return this.formSuccee();
	}
	
	
	
	@RequestMapping(value = "delete")
	public String delete(Role role,HttpServletRequest request,HttpServletResponse response){
		return this.formSuccee();
	}
	
	@RequestMapping(value = "tree")
	public String tree(User user,HttpServletRequest request,HttpServletResponse response){
		
		UserRole userRole = new UserRole();
		userRole.setUserId(user.getId());
		List<UserRole> urlist = userRoleService.findByList(userRole);
		
		List<Integer> rids = new ArrayList<Integer>();
		for(UserRole ur:urlist){
			rids.add(ur.getRoleId());
		}
		
		List<Role> rlist =  roleService.findByList(new Role());
		List<ZtreeModel> zlist = new ArrayList<ZtreeModel>();
		for(Role r:rlist){
			ZtreeModel zm = new ZtreeModel();
			zm.setpId(0);
			zm.setId(r.getId());
			if(rids.contains(r.getId())){
				zm.setChecked(true);
			}
			zm.setIcon("img/icon/3.png");
			zm.setName(r.getName());
			zlist.add(zm);
		}

		request.setAttribute("json",JsonUtils.ObjectToString(zlist));
		
		return "role/tree";
	}

}
